package com.infra.server.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infra.server.entity.SysUser;
import com.infra.server.entity.SysUserAuth;
import com.infra.server.entity.vo.SysUserVo;
import com.infra.server.mapper.SysUserAuthMapper;
import com.infra.server.mapper.SysUserMapper;
import com.infra.server.service.AsyncTask;
import com.infra.server.service.SysUserAuthService;
import com.infra.server.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: gisocn
 * @Date: 2020/9/30
 * @Description:
 **/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserAuthMapper sysUserAuthMapper;
    @Resource
    private AsyncTask asyncTask;
    @Resource
    private SysUserAuthService sysUserAuthService;

    @Override
    public IPage<SysUserVo> getUserList(IPage<SysUserVo> page,String content) {
        if (StrUtil.isEmpty(content)) {
            return this.baseMapper.getUserList2(page);
        } else {
            content = "%"+content+"%";
            return this.baseMapper.getUserList3(page,content);
        }
    }

    @Override
    public List<SysUserVo> getUserList() {
        return this.baseMapper.getUserList();
    }

    // 事务处理插入用户数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addUser(SysUser sysUser, SysUserAuth sysUserAuth) {
        sysUserMapper.insert(sysUser);
        sysUserAuth.setUserId(sysUser.getId());
        return sysUserAuthMapper.insert(sysUserAuth);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateUser(SysUser sysUser, SysUserAuth sysUserAuth) {
        sysUserMapper.updateById(sysUser);
        QueryWrapper<SysUserAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",sysUserAuth.getUsername());
        return sysUserAuthMapper.update(sysUserAuth,queryWrapper);
    }

    @Override
    public int delUser(Long id) {
        // 真实删除
        //        return this.baseMapper.delUser(id);
        // 形式上删除
        UpdateWrapper<SysUserAuth> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",id).set("is_del",1);
        boolean update = sysUserAuthService.update(updateWrapper);
        return update ? 1 : 0;
    }

    @Override
    public List<Integer> getRole(Long userId) {
        return this.baseMapper.getRole(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userAssignRole(Long userId, List<Integer> roleId) {
        // 修改用户类型
        asyncTask.updateUserType(userId, roleId);
        if (ObjectUtil.isEmpty(roleId)) {
            return this.baseMapper.delUserRole(userId);
        } else {
            // 先删除此用户全部角色
            this.baseMapper.delUserRole(userId);
            return this.baseMapper.addUserRole(userId,roleId);
        }
    }


}
